package com.wykj.business.dao;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.HTablePool;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes;

public class HbaseDao {
	public static Configuration configuration;  
    static {  
        configuration = HBaseConfiguration.create();  
        configuration.set("hbase.zookeeper.property.clientPort", "2181");  
        configuration.set("hbase.zookeeper.quorum", "192.168.1.100");  
        configuration.set("hbase.master", "192.168.1.100:600000");  
    }  
  
    public static void main(String[] args) {  
        // createTable("wujintao");  
        // insertData("wujintao");  
        // QueryAll("wujintao");  
        // QueryByCondition1("wujintao");  
        // QueryByCondition2("wujintao");  
        //QueryByCondition3("wujintao");  
        //deleteRow("wujintao","abcdef");  
        deleteByCondition("wujintao","abcdef");  
    }  
  
    /** 
     * 创建表 
     * @param tableName 
     */  
    public static void createTable(String tableName) {  
        System.out.println("start create table ......");  
        try {  
            HBaseAdmin hBaseAdmin = new HBaseAdmin(configuration);  
            if (hBaseAdmin.tableExists(tableName)) {// 如果存在要创建的表，那么先删除，再创建  
                hBaseAdmin.disableTable(tableName);  
                hBaseAdmin.deleteTable(tableName);  
                System.out.println(tableName + " is exist,detele....");  
            }  
            HTableDescriptor tableDescriptor = new HTableDescriptor(tableName);  
            tableDescriptor.addFamily(new HColumnDescriptor("column1"));  
            tableDescriptor.addFamily(new HColumnDescriptor("column2"));  
            tableDescriptor.addFamily(new HColumnDescriptor("column3"));  
            hBaseAdmin.createTable(tableDescriptor);  
        } catch (MasterNotRunningException e) {  
            e.printStackTrace();  
        } catch (ZooKeeperConnectionException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        System.out.println("end create table ......");  
    }  
  
    /** 
     * 插入数据 
     * @param tableName 
     */  
    public static void insertData(String tableName) {  
        System.out.println("start insert data ......");  
        HTablePool pool = new HTablePool(configuration, 1000);  
        HTable table = (HTable) pool.getTable(tableName);  
        Put put = new Put("112233bbbcccc".getBytes());// 一个PUT代表一行数据，再NEW一个PUT表示第二行数据,每行一个唯一的ROWKEY，此处rowkey为put构造方法中传入的值  
        put.add("column1".getBytes(), null, "aaa".getBytes());// 本行数据的第一列  
        put.add("column2".getBytes(), null, "bbb".getBytes());// 本行数据的第三列  
        put.add("column3".getBytes(), null, "ccc".getBytes());// 本行数据的第三列  
        try {  
            table.put(put);  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        System.out.println("end insert data ......");  
    }  
  
    /** 
     * 删除一张表 
     * @param tableName 
     */  
    public static void dropTable(String tableName) {  
        try {  
            HBaseAdmin admin = new HBaseAdmin(configuration);  
            admin.disableTable(tableName);  
            admin.deleteTable(tableName);  
        } catch (MasterNotRunningException e) {  
            e.printStackTrace();  
        } catch (ZooKeeperConnectionException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
  
    }  
    /** 
     * 根据 rowkey删除一条记录 
     * @param tablename 
     * @param rowkey 
     */  
     /*public static void deleteRow(String tablename, String rowkey)  {  
        try {  
            HTable table = new HTable(configuration, tablename);  
            List list = new ArrayList();  
            Delete d1 = new Delete(rowkey.getBytes());  
            list.add(d1);  
              
            table.delete(list);  
            System.out.println("删除行成功!");  
              
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
  
    }*/  
  
     /** 
      * 组合条件删除 
      * @param tablename 
      * @param rowkey 
      */  
     public static void deleteByCondition(String tablename, String rowkey)  {  
            //目前还没有发现有效的API能够实现 根据非rowkey的条件删除 这个功能能，还有清空表全部数据的API操作  
  
    }  
  
  
    /** 
     * 查询所有数据 
     * @param tableName 
     */  
    public static void QueryAll(String tableName) {  
        HTablePool pool = new HTablePool(configuration, 1000);  
        HTable table = (HTable) pool.getTable(tableName);  
        try {  
            ResultScanner rs = table.getScanner(new Scan());  
            for (Result r : rs) {  
                System.out.println("获得到rowkey:" + new String(r.getRow()));  
                for (KeyValue keyValue : r.raw()) {  
                    System.out.println("列：" + new String(keyValue.getFamily())  
                            + "====值:" + new String(keyValue.getValue()));  
                }  
            }  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
  
    /** 
     * 单条件查询,根据rowkey查询唯一一条记录 
     * @param tableName 
     */  
    public static void QueryByCondition1(String tableName) {  
  
        HTablePool pool = new HTablePool(configuration, 1000);  
        HTable table = (HTable) pool.getTable(tableName);  
        try {  
            Get scan = new Get("abcdef".getBytes());// 根据rowkey查询  
            Result r = table.get(scan);  
            System.out.println("获得到rowkey:" + new String(r.getRow()));  
            for (KeyValue keyValue : r.raw()) {  
                System.out.println("列：" + new String(keyValue.getFamily())  
                        + "====值:" + new String(keyValue.getValue()));  
            }  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
  
    /** 
     * 单条件按查询，查询多条记录 
     * @param tableName 
     */  
    /*public static void QueryByCondition2(String tableName) {  
  
        try {  
            HTablePool pool = new HTablePool(configuration, 1000);  
            HTable table = (HTable) pool.getTable(tableName);  
            Filter filter = new SingleColumnValueFilter(Bytes  
                    .toBytes("column1"), null, CompareOp.EQUAL, Bytes  
                    .toBytes("aaa")); // 当列column1的值为aaa时进行查询  
            Scan s = new Scan();  
            s.setFilter(filter);  
            ResultScanner rs = table.getScanner(s);  
            for (Result r : rs) {  
                System.out.println("获得到rowkey:" + new String(r.getRow()));  
                for (KeyValue keyValue : r.raw()) {  
                    System.out.println("列：" + new String(keyValue.getFamily())  
                            + "====值:" + new String(keyValue.getValue()));  
                }  
            }  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
  
    }  
  
    *//** 
     * 组合条件查询 
     * @param tableName 
     *//*  
    public static void QueryByCondition3(String tableName) {  
  
        try {  
            HTablePool pool = new HTablePool(configuration, 1000);  
            HTable table = (HTable) pool.getTable(tableName);  
  
            List<Filter> filters = new ArrayList<Filter>();  
  
            Filter filter1 = new SingleColumnValueFilter(Bytes  
                    .toBytes("column1"), null, CompareOp.EQUAL, Bytes  
                    .toBytes("aaa"));  
            filters.add(filter1);  
  
            Filter filter2 = new SingleColumnValueFilter(Bytes  
                    .toBytes("column2"), null, CompareOp.EQUAL, Bytes  
                    .toBytes("bbb"));  
            filters.add(filter2);  
  
            Filter filter3 = new SingleColumnValueFilter(Bytes  
                    .toBytes("column3"), null, CompareOp.EQUAL, Bytes  
                    .toBytes("ccc"));  
            filters.add(filter3);  
  
            FilterList filterList1 = new FilterList(filters);  
  
            Scan scan = new Scan();  
            scan.setFilter(filterList1);  
            ResultScanner rs = table.getScanner(scan);  
            for (Result r : rs) {  
                System.out.println("获得到rowkey:" + new String(r.getRow()));  
                for (KeyValue keyValue : r.raw()) {  
                    System.out.println("列：" + new String(keyValue.getFamily())  
                            + "====值:" + new String(keyValue.getValue()));  
                }  
            }  
            rs.close();  
  
        } catch (Exception e) {  
            e.printStackTrace();  
        } 
    } */
    
    
    //删除row  
    /*public static boolean deleteRow(String tableName, String rowName) throws IOException {  
        HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();  
        Table table = connection.getTable(TableName.valueOf(tableName));  
        if (admin.tableExists(tableName)) {  
            try {  
                Delete delete = new Delete(rowName.getBytes());  
                table.delete(delete);  
            } catch (Exception e) {  
                e.printStackTrace();  
                return false;  
            }  
        }  
        return true;  
    }  */
  
    //删除qualifier  
    /*public static boolean deleteQualifier(String tableName, String rowName, String columnFamilyName, String qualifierName) throws IOException {  
        HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();  
        Table table = connection.getTable(TableName.valueOf(tableName));  
        if (admin.tableExists(tableName)) {  
            try {  
                Delete delete = new Delete(rowName.getBytes());  
                delete.addColumns(columnFamilyName.getBytes(), qualifierName.getBytes());  
                table.delete(delete);  
            } catch (Exception e) {  
                e.printStackTrace();  
                return false;  
            }  
        }  
        return true;  
    }  */
  
    //单个qualifier的值等于  
    /*public static String queryequal(String tablename, String columnFamily, String qualifier, String data) throws Exception {  
        //某列等于data的  
        Filter filter = new SingleColumnValueFilter(Bytes.toBytes(columnFamily), Bytes.toBytes(qualifier),  
                CompareFilter.CompareOp.EQUAL, Bytes.toBytes(data));  
        FilterList filterList = new FilterList();  
        filterList.addFilter(filter);  
  
        return query(tablename, filterList);  
    } */ 
  
    //查询年龄在mindata和maxdata之间的数据  
    /*public static String queryagebetween(String tablename, String columnFamily, String qualifier, String mindata, String maxdata) throws Exception {  
        Filter filter = new SingleColumnValueFilter(Bytes.toBytes(columnFamily), Bytes.toBytes(qualifier),  
                CompareFilter.CompareOp.LESS_OR_EQUAL, Bytes.toBytes(maxdata));  
        Filter filter1 = new SingleColumnValueFilter(Bytes.toBytes(columnFamily), Bytes.toBytes(qualifier),  
                CompareFilter.CompareOp.GREATER_OR_EQUAL, Bytes.toBytes(mindata));  
        FilterList filterList = new FilterList();  
        filterList.addFilter(filter);  
        filterList.addFilter(filter1);  
        return query(tablename, filterList);  
    }  */
  
    //过滤列  
    /*public static String querycolumn(String tablename, String prefix) throws Exception {  
        Filter filter = new ColumnPrefixFilter(Bytes.toBytes(prefix));  
        FilterList filterList = new FilterList();  
        filterList.addFilter(filter);  
        return query(tablename, filterList);  
    }  */
  
    //得到分页  
    /*public static String queryRowCount(String tablename, String startrowname, String count) throws Exception {  
        Filter filter = new PageFilter(Integer.parseInt(count));  
        Filter filter1 = new SingleColumnValueFilter(Bytes.toBytes("author"), Bytes.toBytes("age"),  
                CompareFilter.CompareOp.EQUAL, Bytes.toBytes("30"));  
        FilterList filterList = new FilterList();  
//        filterList.addFilter(filter1);  
        filterList.addFilter(filter);  
  
        Table table = connection.getTable(TableName.valueOf(tablename));  
        Scan s = new Scan(Bytes.toBytes(startrowname));  
        s.setFilter(filterList);  
        ResultScanner rs = table.getScanner(s);  
  
        List<Map<String, Object>> resList = new ArrayList<Map<String, Object>>();  
        for (Result r : rs) {  
            Map<String, Object> tempmap = resultToMap(r);  
            resList.add(tempmap);  
        }  
  
        return resList.toString();  
    }  
  
    //所有查询都需要的公共query方法  
    public static String query(String tablename, FilterList filterList) throws Exception {  
        Table table = connection.getTable(TableName.valueOf(tablename));  
        Scan s = new Scan();  
        s.setFilter(filterList);  
        ResultScanner rs = table.getScanner(s);  
  
        List<Map<String, Object>> resList = new ArrayList<Map<String, Object>>();  
        for (Result r : rs) {  
            Map<String, Object> tempmap = resultToMap(r);  
            resList.add(tempmap);  
        }  
        return resList.toString();  
    }  */
}
